// pages/bindClassIntroduce/bindClassIntroduce.js
let request = require('../../operation/operation.js')
let klassModel = require('../../models/klass.js')
let noticeModel = require('../../models/notice.js')
let bxt = require('../../utils/bxt.js')
let klass = null 

Page({

  /**
   * 页面的初始数据
   */
  data: {
    klass:{}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    klass = getApp().globalData.param
    this.setData({
        klass:klass
    })
  },


  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  onBack:function() {
    wx.navigateBack({
      
    })
  },

  onCreateActivateMessage:function() {
      let notice = noticeModel.getActivateNotice(klass.sid)

      if (null == notice) {
          this.createActivateNotice()
      }else {
          getApp().globalData.param = {'notice':notice,'klass':klass}
          wx.navigateTo({
              url: '../noticePreview/noticePreview?activateKlass=true',
          })
      }
  },

  /**
   * 创建绑定班级的通知
   */
  createActivateNotice:function() {
    let that = this
    let params = { 'category': 101, 'klassSid': klass.sid}

    request.postRequest('/notices', params, true)
      .then(res => {
          var data = res.data;
          if (request.SUCCESSED == data.status) {
              getApp().notificationCenter.post(bxt.REFRESH_NOTICES_MESSAGE)
              getApp().globalData.param = data.object
              wx.navigateTo({
                  url: '../noticePreview/noticePreview?activateKlass=true',
              })
          } else {
              wx.showToast({
                  title: data.msg,
                  iocn: 'none'
              })
          }
      })
      .catch(e => {
          console.log(e)
      })
  }
})